package com.luyuan.crm.user.web;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.hibernate.ObjectNotFoundException;
import org.springframework.stereotype.Component;

import com.luyuan.crm.user.PartyGroup;
import com.luyuan.crm.user.service.PartyGroupService;
import com.ruijie.util.StringUtils;

@Component("partyGroupConverter")
public class PartyGroupConverter implements Converter {
    @Resource(name = "partyGroupService")
    private PartyGroupService partyGroupService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }
        return partyGroupService.getById(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }

        PartyGroup partyGroup = (PartyGroup) value;
        try {
            return partyGroup.getId();
        } catch (ObjectNotFoundException e) {
            return null;
        }
    }

}
